import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User } from 'src/db/schemas/user.schemas';
import { Model } from 'mongoose';
import { Captcha, RegistUserDto } from './dto/regist-user.dto';
import * as svgCaptcha from 'svg-captcha'


@Injectable()
export class AuthService {
    private captchas: Map<string, Captcha> = new Map()

  constructor(
    @InjectModel('USER_MODEL') private readonly userModel: Model<User>,
  ) {}

  public getCaptcha(): Captcha {
      let getdiffcaptcha = false;
      let c: svgCaptcha.CaptchaObj;
      let captcha: Captcha;
      while (!getdiffcaptcha) {
          c = svgCaptcha.create();
          if (!this.captchas.has(c.text)) {
              captcha = Object.assign(c, { time: new Date() });
              getdiffcaptcha = true;
              this.captchas.set(c.text, captcha)
          }
      }
      return captcha
  }

  public registUser(registUserDto: RegistUserDto) {
      // 1.检查验证码是否过期
      // 2.检查验证码是否正确
      // 3.检查用户名是否存在
      // 4.检查密码是否过于简单
      // 5.注册用户

  }
}
